<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tree-table</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css">
    <link rel="stylesheet" href="css/common.css"/>
    <style>
        input {
            height: 33px;
            line-height: 33px;
            padding: 0 7px;
            border: 1px solid #ccc;
            border-radius: 2px;
            margin-bottom: -2px;
            outline: none;
        }

        input:focus {
            border-color: #009E94;
        }
    </style>
</head>
<body>
<div class="layui-container layui-text" style="width: 100%;" >
    <script type="text/html" id="toolbarDemo">
        <div class="">
            搜索： <input id="edt-search" type="seach" placeholder="输入关键字" autocomplete="off" style="width: 120px;"/>
            <button class="layui-btn" lay-event="btn-search">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
            <button class="layui-btn" lay-event="add">增加数据</button>
            <button class="layui-btn" lay-event="btn-expand">全部展开</button>
            <button class="layui-btn" lay-event="btn-fold">全部折叠</button>
            <button class="layui-btn" lay-event="btn-refresh">刷新表格</button>
            <button class="layui-btn" lay-event="btn-dao">导出数据</button>
            <!--<input type="button" lay-event="btn-search" class="layui-btn" value="搜索" />-->
        </div>
    </script>
    <table id="table1" class="layui-table" lay-filter="table1"></table>

</div>
<!-- 操作列 -->
<script type="text/html" id="oper-col">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../../res/layui/layui.js"></script>
<script>
    var renderTable = null ;
    var table = null ;
    layui.config({
        base: 'module/'
    }).extend({
        treetable: 'treetable-lay/treetable',
    }).use(['layer', 'table', 'treetable' ], function () {
        var $ = layui.jquery;
        table = layui.table;
        var layer = layui.layer;
        var treetable = layui.treetable;
        // 渲染表格
        renderTable = function () {
            layer.load(2);
            treetable.render({
                toolbar: '#toolbarDemo' ,//开启头部工具栏，并为其绑定左侧模板
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'itemId',
                treePidName: 'parentId',
                treeDefaultClose: true,
                treeLinkage: false,
                elem: '#table1',
                url: '../../atsBaseItem/selectJs?isva=2',
                page: true,
                cols: [[
                    {type: 'numbers'},
                    {field: 'itemExp', title: '指标名称' , width:'32%'},
                    {field: 'parentName', title: '上级指标', width:'20%'},
                    {field: 'itemLevel', title: '指标层级' , width:'10%'},
                    {field: 'itemUnit', title: '指标单位' , width:'10%'},
                    {field: 'remark', title: '备注' , width:'10%'},
                    {templet: '#oper-col', title: '操作' , width:'13%'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };

        renderTable();



        //监听工具条
        table.on('tool(table1)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {

                var urls = "../../atsBaseItem/findItme";
                var datae = {id:data.id};
                $.post(urls,datae,function(objs){
                    if(objs.num>0){
                        layer.confirm('该指标绑定了体育项目，是否确认删除',function(){
                            var url = "../../atsBaseItem/delete";
                            var datas = {id:data.id,isva:1};
                            $.post(url,datas,function(obj){
                                if(obj==1){
                                    layer.msg('删除成功');
                                    renderTable();
                                }else{
                                    layer.msg('删除失败');
                                }
                            },'json')
                        })
                    }else{
                        layer.confirm('是否删除',function(){
                            var url = "../../atsBaseItem/delete";
                            var datas = {id:data.id};
                            $.post(url,datas,function(obj){
                                if(obj==1){
                                    layer.msg('删除成功');
                                    renderTable();
                                }else{
                                    layer.msg('删除失败');
                                }
                            },'json')
                        })
                    }
                },'json')
            } else if (layEvent === 'edit') {
                layer.open({
                    type: 2,
                    title: '数据',
                    closeBtn: 1, //不显示关闭按钮
                    shade: [0],
                    area: ['80%', '90%'],
                    anim: 2,
                    content: ['atsBaseItemAddOrUpdateJH.html?id='+data.id, 'no'], //iframe的url，no代表不显示滚动条

                });
            }
        });
        table.on('toolbar(table1)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    var data = checkStatus.data;
                    layer.open({
                        type: 2,
                        title: '数据',
                        closeBtn: 1, //不显示关闭按钮
                        shade: [0],
                        area: ['80%', '90%'],
                        anim: 2,
                        content: ['atsBaseItemAddOrUpdateJH.html', 'no'], //iframe的url，no代表不显示滚动条
                    });
                    break;
                case 'btn-expand':
                    treetable.expandAll('#table1');
                    break ;
                case 'btn-fold':
                    treetable.foldAll('#table1');
                    break ;
                case 'btn-refresh':
                    renderTable();
                    break ;
                case 'btn-dao':
                    location.href="../../atsBaseItem/export?isva=1";
                    break ;
                case 'btn-search':
                    var keyword = $('#edt-search').val();
                    var searchCount = 0;
                    $('#table1').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
                        $(this).css('background-color', 'transparent');
                        var text = $(this).text();
                        if (keyword != '' && text.indexOf(keyword) >= 0) {
                            $(this).css('background-color', 'rgba(250,230,160,0.5)');
                            if (searchCount == 0) {
                                treetable.expandAll('#table1');
                                $('html,body').stop(true);
                                $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
                            }
                            searchCount++;
                        }
                    });
                    if (keyword == '') {
                        layer.msg("请输入搜索内容", {icon: 5});
                    } else if (searchCount == 0) {
                        layer.msg("没有匹配结果", {icon: 5});
                    }
                    break ;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });
    });
</script>
</body>
</html>
